#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "..//include//createvfs.h"
#include "..//include//n_ary.h"
#include "..//include//vfs_errorcodes.h"
#define pf printf


int makedirectory(char[],char[],char[]);
struct fdesc
{
char name[100];
int e;
};

int makedirectory(char parent_path[],char dir_name[],char filename_g[])
{
char temp[50];

int i,j=0,s,f1=1,f2=0,c=1,l,n=0,pos,cl,p[50],md=0,l1,k,len,ud;
FILE *f;
struct header h;
struct fdesc fd[30];
f=fopen(filename_g,"rb+");
fread(&h,sizeof(struct header),1,f);
fclose(f);
l=strlen(dir_name);
l1=strlen(parent_path);
if(l==0||l1==0)
{
pf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_00);
  return 0;

}
if(parent_path[l1-1]!='/')
{
strcat(parent_path,"/");
}

if ((strcmp(parent_path,"") == 0) || (strcmp(dir_name,"") == 0)){
  pf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_00);
  return 0;
}


if(h.used_desc==TOTAL_DESC){
  pf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_01);
  return 0;
}

l=strlen(dir_name);


if(strstr(dir_name,"/"))
{
pf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_02);
return(0);
}




l=strlen(dir_name);
l1=strlen(parent_path);
for(i=0;i<50;i++)
p[i]=0;

while(parent_path[c]!='\0')
{
if((parent_path[c]=='/'))
{
p[j]=c;
j++;
}
c++;
}


if(j>0)
{
md=1;
}


if(md==0)
{
p[0]=0;
}
n=0;

for(i=0;(md==1&&p[i]!=0);i++)
{
c=0;
for(j=0;j<=p[i];j++)
{
fd[n].name[j]=parent_path[j];
c++;
}
fd[n].name[c]='\0';
fd[n].e=0;
n++;
}

i=0;
/*if(md==1)
{
for(i=0;i<n;i++)
pf("-------------n=%d%s\n",i,fd[i].name);

}*/


for(j=0;j<n&&md==1;j++)
{
strcpy(temp,"\0");
//strcpy(temp,parent_path);
strcpy(temp,fd[j].name);

for(i=1;i<TOTAL_DESC;i++)
{
s=strcmp(temp,h.fd_arr[i].path);
if(s==0)
{
fd[j].e=1;
break;
}

}

}
strcpy(temp,"\0");




c=1;
/*if(strstr(dir_name,"/"))
{
return ERR_VFS_MAKEDIR_02;
}*/






strcpy(temp,"/0");
strcpy(temp,parent_path);
strcat(temp,dir_name);
strcat(temp,"/");

//pf("\nmd=%d\nn=%d",md,n);
while(1)
{

for(i=1;i<TOTAL_DESC;i++)
{
s=strcmp(h.fd_arr[i].path,temp);
if(s==0)
f1=0;
}

if(f1==0)
{
pf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_03);
return(0);
break;
}

/*for(i=1;i<TOTAL_DESC;i++)
{
s=strcmp(h.fd_arr[i].path,parent_path);
if(s==0)
f2=1;
}

if(f2!=1)
{
pf("makedir_FAILURE\t%s",ERR_VFS_MAKEDIR_01);
return(0);
break;
}*/

if(f1==1&&md==0)
{
for(i=1;i<TOTAL_DESC;i++)
{
s=strcmp(h.fd_arr[i].path,"\0");
if(s==0)
{
strcpy(h.fd_arr[i].name,dir_name);
strcpy(h.fd_arr[i].path,temp);
ud=h.used_desc;
ud = ud + 1;
h.used_desc = ud;
root=insert(root,temp);
//pf("\nDIRECTORY ADDED SUCESSFULLY\n");
break;
}
}
}

if(f1==1&&md==1)
{
for(j=0;j<n;j++)
{
strcpy(temp,"");
//strcpy(temp,parent_path);
strcpy(temp,fd[j].name);
root=insert(root,temp);
for(i=1;i<TOTAL_DESC;i++)
{
s=strcmp(h.fd_arr[i].path,"\0");
c=0;
if(s==0&&fd[j].e!=1)
{
strcpy(h.fd_arr[i].path,fd[j].name);
if(j==0)
{
for(k=1;k<p[j];k++)
{
h.fd_arr[i].name[c]=fd[j].name[k];
c++;
}
h.fd_arr[i].name[c]='\0';
}
else
{
for(k=p[j-1]+1;k<p[j];k++)
{
h.fd_arr[i].name[c]=fd[j].name[k];
c++;
}
h.fd_arr[i].name[c]='\0';
}
ud=h.used_desc;
ud = ud + 1;
h.used_desc = ud;
//pf("\nDIRECTORY ADDED SUCESSFULLY\n");
break;
}
}


}
break;
}


break;

}

if(md==1&&f1==1)
{
strcpy(temp,"");
strcpy(temp,parent_path);
strcat(temp,dir_name);
strcat(temp,"/");
//root=insert(root,temp);


for(i=1;i<TOTAL_DESC;i++)
{
s=strcmp(h.fd_arr[i].path,"\0");
if(s==0)
{
strcpy(h.fd_arr[i].name,dir_name);
strcpy(h.fd_arr[i].path,temp);
ud=h.used_desc;
ud = ud + 1;
h.used_desc = ud;
root=insert(root,temp);
//pf("\nDIRECTORY ADDED SUCESSFULLY\n");
break;
}
}


}




f=fopen(filename_g,"rb+");
rewind(f);
fwrite(&h,sizeof(struct header),1,f);
fclose(f);
return(1);

}

